Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Override MessageEventTarget's events' this-parameter type #1882

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

sandersn
Copy link
Member

This adds the ability to override the this parameter of a type's event handlers, which I need for MessageEventTarget to avoid breaking code.
Technically, I believe this: this is correct for all event handlers, but it's expensive to compile and confusing to understand so I'd prefer to use it only when needed.

I don't know if this is the right way to do this in the generator code. Corrections welcome.

Copy link
Contributor

Thanks for the PR!

This section of the codebase is owned by @saschanaz - if they write a comment saying "LGTM" then it will be merged.

@saschanaz
Copy link
Contributor

Probably better to autodetect whether there's inheritance/mixin relationship and use this automatically in such case, wdyt? (In that case anything related to HTMLElement should be excluded for now or otherwise all the HTML event handlers would get this.)

@saschanaz
Copy link
Contributor

But in case this is urgent I think this is fine too.

@sandersn
Copy link
Member Author

sandersn commented Jan 21, 2025

I think that's theoretically a good idea. But .. how many inheritance/mixin relationships are there? Even though this: this is correct I'd prefer to default it off for performance reasons.

If we do this, would you recommend putting the HTMLElement exclusion in overridingtypes.jsonc or hard-coded in the generator?

Edit: looks like there are around 120 mixins.

@sandersn
Copy link
Member Author

However, when I add manual exclusions, there are only 3 of them: AbstractWorker, GlobalEventHandlers and WindowEventHandlers. I'll try to come up with a property for overridingTypes.jsonc.

Copy link
Contributor

@saschanaz saschanaz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should probably apply to interfaces inherited by others, but looks good

@sandersn
Copy link
Member Author

sandersn commented Jan 21, 2025

Unfortunately, this solution still makes Worker invariant and breaks on examples like partytown's tests:

interface PartytownWorker {
  // ...
}
interface TestWorker extends PartytownWorker {
  messages: any[]
}
// but you can't use a TestWorker as a PartytownWorker
// because this: this is ultimately invariant
declar var tw: TestWorker
var ptw: PartytownWorker = tw

@rbuckton suggested making MessageEventTarget generic so that Worker and the other extenders can specify a fixed this type:

interface MessageEventTarget<T> {
  onmessage: ((this: T, ...
}
interface Worker extends MessageEventTarget<Worker> {
}

This is a delimited form of this: this that stops with Worker (and GlobalEventHandler and WindowEventHandler). I'll see if I can figure out how to emit this.

@sandersn
Copy link
Member Author

microsoft/TypeScript#60987 is the PR I'm using to test this on TS.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants